
package coms.dataaccess.hibernate;

import coms.dataaccess.dao.EventDAO;
import coms.dto.Event;
import coms.dto.Event;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class EventDAOImpl extends GenericDAOImpl<Event, Integer> implements EventDAO{

    private Session session;
    
    @Override
    public List<Event> search(Event event) throws SQLException {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Event.class, "event");
        if (event == null) return criteria.list();
        
         if (event.getName()!=null && !event.getName().equals("")){
            criteria.add(Restrictions.like("event.name", "%"+event.getName()+"%"));
        }
        
        if (event.getConference()!= null){
            criteria.createAlias("event.conference", "conf")
                    .add(Restrictions.eq("conf.id", event.getConference().getId()));
        }
        List<Event> eventList = criteria.list();
        clear();
        return eventList;
    
    }
    
}
